<?php
/**
 * User: hjd-yao
 * Date: 2019/4/23
 * Time: 16:53
 */

namespace App\Http\Controllers\Store;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Shop\ShopCategoryModel;
use App\Models\Store\ShopStoreInfoModel;
use App\Models\Store\ShopStoreModel;
use Illuminate\Http\Request;

class StoreController extends BackendController
{
    /**
     * @title 店铺列表
     * @param Request $request
     * @return View
     */
    public function stores(Request $request)
    {
        $list = ShopStoreModel::with('user')->whereIn('status',[0,1])
            ->orderBy('status')->paginate($this->getPerPageNum());
        return view('backend::shop.store.store_list',[
            'page_title'=>'店铺列表',
            'list'=>$list
        ]);
    }

    /**
     * @title 已关闭店铺
     * @param Request $request
     * @return View
     */
    public function closedStores(Request $request)
    {
        $list = ShopStoreModel::with('user')
            ->where('status',-1)->paginate($this->getPerPageNum());
        return view('backend::shop.store.store_closed',[
            'page_title'=>'关停店铺',
            'list'=>$list
        ]);
    }

    /**
     * @title 添加店铺
     * @param Request $request
     * @return SelfResponse|View
     */
    public function create(Request $request)
    {
        if($request->isMethod('post')){
            $store = new ShopStoreModel();
            if(!$store->addStore($request)){
                return SelfResponse::error($store->getErrorMsg('添加商铺失败'));
            }
            return SelfResponse::success('店铺添加成功','b_Store_stores');
        }else{
            if(!ShopCategoryModel::first()){
                return redirect()->route('b_ShopCategory_create');
            }
            return view('backend::shop.store.store_add',[
                'page_title'=>'添加店铺',
                'category'=>json_encode(ShopCategoryModel::init()->getTrees()),
            ]);
        }
    }

    /**
     * @title 更新店铺信息
     * @param Request $request
     * @param $store_id
     * @return SelfResponse|View
     */
    public function update(Request $request,$store_id)
    {
        if($request->isMethod('post')){
            $store = new ShopStoreModel();
            if(!$store->editStore($request,$store_id)){
                return SelfResponse::error($store->getErrorMsg('编辑失败'));
            }
            return SelfResponse::success('编辑成功','b_Store_stores');
        }else{
            $store = ShopStoreModel::find($store_id);
            return view('backend::shop.store.store_edit',[
                'page_title'=>'编辑'.$store->store_name,
                'store'=>$store,
                'category'=>json_encode(ShopCategoryModel::init()->getTrees()),
            ]);
        }
    }

    /**
     * @title 删除店铺
     * @param $store_id
     * @return SelfResponse|JsonResponse
     */
    public function delete($store_id)
    {
        $store = new ShopStoreModel();

        if(!$store->deleteStore($store_id)){
            return SelfResponse::error($store->getErrorMsg('删除失败'));
        }

        return SelfResponse::error('删除成功!');

    }

    /**
     * @title 批量删除店铺
     * @param Request $request
     * @return SelfResponse
     */
    public function batchDelete(Request $request)
    {
        $store_ids = array_filter($request->get('store_ids'));
        if(!$store_ids){
            return SelfResponse::error('请选择要操作的数据!');
        }

        if(ShopStoreModel::whereIn('store_id',$store_ids)->delete()){
            ShopStoreInfoModel::whereIn('store_id',$store_ids)->delete();
            return SelfResponse::success('删除成功!','b_Store_stores');
        }
        return SelfResponse::error('删除失败');
    }

    /**
     * @title 店铺详情
     * @param $store_id
     * @return View
     */
    public function detail($store_id)
    {
        $store = ShopStoreModel::find($store_id);
        $category = ShopCategoryModel::whereIn('id',str2arr($store->category_ids))->pluck('name','id');
        return view('backend::shop.store.store_detail',[
            'page_title'=>$store->store.'详情',
            'store'=>$store,
            'category'=>$category,
        ]);
    }

    /**
     * @title 关停店铺
     * @param Request $request
     * @return SelfResponse
     */
    public function closeStore(Request $request)
    {
        $store_ids = array_filter($request->post('store_ids'));
        if(!$store_ids){
            return SelfResponse::error('请选择要操作的数据');
        }
        if(!ShopStoreModel::whereIn('store_id',$store_ids)->update(['status'=>-1])){
            return SelfResponse::error('店铺关停失败,请稍后再试');
        }
        return SelfResponse::success('店铺已成功关停!','b_Store_closedStores');
    }

    /**
     * @title 启用店铺
     * @param Request $request
     * @return SelfResponse
     */
    public function openStore(Request $request)
    {
        $store_ids = array_filter($request->post('store_ids'));
        if(!$store_ids){
            return SelfResponse::error('请选择要操作的数据');
        }
        if(!ShopStoreModel::whereIn('store_id',$store_ids)->update(['status'=>1])){
            return SelfResponse::error('店铺启用失败,请稍后再试');
        }
        return SelfResponse::success('店铺启用成功!','b_Store_stores');
    }
}